﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Params.SmtpEnabled)
        {
            this.hFw.Visible = false;
        }
        if (Request.Cookies["UserCode"]!= null)
        {
            string uc = Server.UrlDecode(Request.Cookies["UserCode"].Value);
            int g = LwUser.GetGuidByUserCode(uc);
            if (g != -1)
            {
                LwUser lu = new LwUser(g);
                Session.Add("Loginer", lu);
                Session.Add(Params.SystemError, "登录成功");
                //this.SetCookie(lu);
                if (lu.UserType == LwUser.AdminType)
                {
                    Response.Redirect(Params.AdminFolderHttp + "settings.aspx?uid=" + Server.UrlEncode(lu.UserID));
                }
                else if (lu.UserType == LwUser.TeamType)
                {
                    Response.Redirect(Params.TeamFolderHttp + "default.aspx?uid=" + Server.UrlEncode(lu.UserID));
                }
                else if (lu.UserType == LwUser.DoctorType)
                {
                    Response.Redirect(Params.DoctorFolderHttp + "default.aspx?uid=" + Server.UrlEncode(lu.UserID));
                }
                else
                {
                    Response.Redirect(Params.StudentFolderHttp + "default.aspx?uid=" + Server.UrlEncode(lu.UserID));
                }
            }
        }
    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        string uid = this.tbID.Text;
        string pwd = this.tbPwd.Text;
        if (uid == "" || pwd == "")
        {
            this.info.SetMessage("用户名和密码不能为空！");
            return;
        }
        int r = LwUser.Login(uid, pwd);
        if (r == -2)
        {
            this.info.SetMessage("密码错误！");
            return;
        }
        else if (r == -1)
        {
            this.info.SetMessage("用户名不存在！");
            return;
        }
        else
        {
            LwUser lu = new LwUser(r);
            Session.Add("Loginer", lu);
            Session.Add(Params.SystemError, "登录成功");
            this.SetCookie(lu);
            if (lu.UserType == LwUser.AdminType)
            {
                Response.Redirect(Params.AdminFolderHttp + "settings.aspx?uid="+Server.UrlEncode(lu.UserID));
            }
            else if (lu.UserType == LwUser.TeamType)
            {
                Response.Redirect(Params.TeamFolderHttp + "default.aspx?uid=" + Server.UrlEncode(lu.UserID));
            }
            else if(lu.UserType==LwUser.DoctorType)
            {
                Response.Redirect(Params.DoctorFolderHttp + "default.aspx?uid=" + Server.UrlEncode(lu.UserID));
            }
            else
            {
                Response.Redirect(Params.StudentFolderHttp + "default.aspx?uid=" + Server.UrlEncode(lu.UserID));
            }
        }
    }

    private void SetCookie(LwUser u)
    {
        int t = Convert.ToInt32(this.ddSave.SelectedValue);
        if (t == 0)
        {
            return;
        }
        string uc = Fun.GetRandomString(32);
        Response.Cookies["UserCode"].Value = Server.UrlEncode(uc);
        u.UserCode = uc;
        u.Update();
        switch (t)
        {
            case 1:
                Response.Cookies["UserCode"].Expires = DateTime.Now.AddMonths(1);
                break;
            case 2:
                Response.Cookies["UserCode"].Expires = DateTime.Now.AddMonths(3);
                break;
            default:
                break;
        }
    }
}
